using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Runtime;
using Godot;
using Godot.Collections;

public partial class ActorBase : Sprite2D, ISave
{
	public int actId { get; }
	public string cName { get; }
	public int cAge { get; }

	public string cDesc { get; }

	public int exp { get; set; }

	public int initExp { get; }

	public int Hp { get; set; }

	public int initHp { get; }
	public int Mp { get; set; }

	public int initMp { get; }

	public int power { get; set; }

	public int initPower { get; }

	public int intelligence { get; set; }

	public int initIntelligence { get; }

	public int agile { get; set; }

	public int initAgile { get; }

	public int defense { get; set; }

	public int initDefense { get; }

	public int jobId { get; set; }

	public int initJobId { get; }

	public int jobLevel { get; set; }

	public int initJobLevel { get; }
	public int moveRange { get; set; }

	public int leftMoveRange { get; set; }

	public System.Collections.Generic.Dictionary<int, ConfigJobLevel> jobLevelData = new System.Collections.Generic.Dictionary<int, ConfigJobLevel>();

	public ActorType actorType { get; set; }
	public bool isInScene { get; set; }

	public ActorState actorState { get; set; }

	public int actionTimes { get; }

	public int leftActionTimes { get; set; }



	public ActorBase(int actorId)
	{
		//获取角色基础配置
		jobLevelData.Clear();
		var allConfig = ConfigManager.getInstance().findAllConfig<ConfigCharacterBaseInfo>("ConfigCharacterBaseInfo");
		foreach (var config in allConfig)
		{
			if (config.ID == actorId)
			{
				actId = config.ID;
				cName = config.cName;
				cAge = config.cAge;
				cDesc = config.cDesc;
				jobId = config.InitJobId;
				exp = config.initExp;
				Hp = config.baseHp;
				Mp = config.baseMp;
				power = config.power;
				intelligence = config.intelligence;
				agile = config.agile;
				defense = config.defense;
				jobLevel = config.InitJobLevel;
				break;
			}
		}
		initExp = exp;
		initHp = Hp;
		initMp = Mp;
		initPower = power;
		initIntelligence = intelligence;
		initAgile = agile;
		initDefense = defense;
		initJobId = jobId;
		initJobLevel = jobLevel;

		//初始化等级配置数据
		var allJobLevelConfigs = ConfigManager.getInstance().findAllConfig<ConfigJobLevel>("ConfigJobLevel");
		foreach (var config in allJobLevelConfigs)
		{
			if (config.jobId == jobId)
			{
				if (config.level == jobLevel)
				{

					Hp = (int)Math.Floor((1 + config.hpPlusRate) * initHp);
					Mp = (int)Math.Floor((1 + config.mpPlusRate) * initMp);
					power = (int)config.powerPlus + initPower;
					intelligence = (int)config.intelligencePlus + initIntelligence;
					agile = (int)config.agilePlus + initAgile;
					defense = (int)config.defensePlus + initDefense;
					moveRange = config.moveRange;
					leftMoveRange = moveRange;
					actionTimes = config.actionTimes;
					leftActionTimes = config.actionTimes;
				}
				jobLevelData.Add(jobLevel, config);
			}

		}

		actorState = ActorState.idle;


		//测试用
		ColorRect cl = new ColorRect();

		cl.Color = new Color((float)GD.RandRange(0, 1), 0, 0, 1);
		AddChild(cl);
		cl.Size = new Vector2(30, 30);


		//挂载button节点，用于触发各种功能
		Button btn = new Button();
		btn.Flat = true;
		btn.Pressed += OnBtnPress;
		btn.Size = TileMapUtil.TileSize;
		AddChild(btn);



	}





	public virtual bool isDead()
	{
		return Hp > 0 ? false : true;
	}


	public virtual Godot.Collections.Dictionary<string, Variant> GetSaveData()
	{

		return new Godot.Collections.Dictionary<string, Variant>(){
			{"exp",exp},
			{"Hp",Hp},
			{"Mp",Mp},
			{"power",power},
			{"intelligence",intelligence},
			{"agile",agile},
			{"defense",defense},
			{"jobId",jobId},
			{"leftMoveRange",leftMoveRange},
			{"jobLevel",jobLevel},
			{"positionX",Position.X},
			{"positionY",Position.Y},
			{"actorType",(int)actorType},
			{"actId",actId},
			{"isInScene",isInScene},
			{"actorState",(int)actorState},
			{"actionTimes",leftActionTimes}
		};
	}

	public virtual void Load(Godot.Collections.Dictionary<string, Variant> data)
	{

		//加载数据
		exp = (int)data["exp"];
		Hp = (int)data["Hp"];
		Mp = (int)data["Mp"];
		power = (int)data["power"];
		intelligence = (int)data["intelligence"];
		agile = (int)data["agile"];
		defense = (int)data["defense"];
		jobId = (int)data["jobId"];
		leftMoveRange = (int)data["leftMoveRange"];
		jobLevel = (int)data["jobLevel"];

		actorType = (ActorType)(int)data["actorType"];
		actorState = (ActorState)(int)data["actorState"];
		leftActionTimes = (int)data["actionTimes"];
		//初始化职业配置信息
		jobLevelData.Clear();

		var allJobLevelConfigs = ConfigManager.getInstance().findAllConfig<ConfigJobLevel>("ConfigJobLevel");
		foreach (var config in allJobLevelConfigs)
		{
			if (config.jobId == jobId)
			{
				jobLevelData.Add(config.level, config);
			}
		}

		//节点的一些动作
		Position = new Vector2((float)data["positionX"], (float)data["positionY"]);

		if ((bool)data["isInScene"])
		{
			SceneUtil.currentScene.AddChild(this);
		}
	}


	protected virtual void OnBtnPress()
	{

		EventParams e = new EventParams();
		e.paramDict["actor"] = this;
		EventManager.getInstance().dispatchEvent(EventType.EVENT_TILE_MAP_ACTOR_CLICK, e);
	}



}

